package com.ruoyi.develop.lnGongyigoods.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.lnGongyi.domain.LnGongyi;
import com.ruoyi.develop.lnGongyi.service.ILnGongyiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lnGongyigoods.domain.LnGongyigoods;
import com.ruoyi.develop.lnGongyigoods.service.ILnGongyigoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品工艺关联Controller
 * @author qh
 * @date 2024-10-23
 */
@RestController
@RequestMapping("/lnGongyigoods/lnGongyigoods")
public class LnGongyigoodsController extends BaseController {
    @Autowired
    private ILnGongyigoodsService lnGongyigoodsService;
    @Autowired
    private ILnGongyiService lnGongyiService;
    @Autowired
    private IGoodsSaleService iGoodsSaleService;
    /**
     * 查询产品工艺关联列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigoods:query')")
    @GetMapping("/list")
    public TableDataInfo list(LnGongyigoods entity) {
        startPage();
        List<LnGongyigoods> list = lnGongyigoodsService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出产品工艺关联列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigoods:export')")
    @Log(title = "产品工艺关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LnGongyigoods entity) {
        List<LnGongyigoods> list = lnGongyigoodsService.selectList(entity);
        ExcelUtil<LnGongyigoods> util = new ExcelUtil<LnGongyigoods>(LnGongyigoods. class);
        util.exportExcel(response, list, "产品工艺关联数据");
    }

    /**
     * 获取产品工艺关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lnGongyigoodsService.selectById(id));
    }

    /**
     * 新增产品工艺关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigoods:add')")
    @Log(title = "产品工艺关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LnGongyigoods entity) {
        return toAjax(lnGongyigoodsService.insert(entity));
    }

    /**
     * 修改产品工艺关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigoods:edit')")
    @Log(title = "产品工艺关联", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LnGongyigoods entity) {
        return toAjax(lnGongyigoodsService.update(entity));
    }

    /**
     * 删除产品工艺关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigoods:remove')")
    @Log(title = "产品工艺关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lnGongyigoodsService.deleteByIds(ids));
    }

    @GetMapping("/addGyGoods")
    public AjaxResult addGyGood(HttpServletRequest request) {
        String goodsId  = request.getParameter("goodsId");
        String gongyiId  = request.getParameter("gongyiId");
        //String saleId  = request.getParameter("saleId");
        //iGoodsSaleService.updateByGongyiId(saleId, gongyiId);
        lnGongyigoodsService.deleteGongyiByGoods(goodsId);
        List<LnGongyigoods> lnGongyigongxuList=lnGongyigoodsService.selectGoodsGongyi(goodsId, gongyiId);
        if(lnGongyigongxuList.size()<1){
            LnGongyigoods lnGongyigoods=new LnGongyigoods();
            LnGongyi lnGongyi=lnGongyiService.selectById(gongyiId);
            lnGongyigoods.setGoodsId(goodsId);
            lnGongyigoods.setGongyiId(gongyiId);
            lnGongyigoods.setCode(lnGongyi.getCode());
            lnGongyigoods.setName(lnGongyi.getName());
            lnGongyigoods.setFile(lnGongyi.getFile());
            lnGongyigoods.setPhoto(lnGongyi.getPhoto());
            lnGongyigoods.setShuoming(lnGongyi.getShuoming());
            lnGongyigoodsService.insert(lnGongyigoods);
        }else if(lnGongyigongxuList.size()==1){
            LnGongyigoods lnGongyigoods=lnGongyigongxuList.get(0);
            LnGongyi lnGongyi=lnGongyiService.selectById(gongyiId);
            lnGongyigoods.setGoodsId(goodsId);
            lnGongyigoods.setGongyiId(gongyiId);
            lnGongyigoods.setCode(lnGongyi.getCode());
            lnGongyigoods.setName(lnGongyi.getName());
            lnGongyigoods.setFile(lnGongyi.getFile());
            lnGongyigoods.setPhoto(lnGongyi.getPhoto());
            lnGongyigoods.setShuoming(lnGongyi.getShuoming());
            lnGongyigoodsService.update(lnGongyigoods);
        }
        return  AjaxResult.success();
    }

    @GetMapping("/addGyGoodsSale")
    public AjaxResult addGyGoodsSale(HttpServletRequest request) {
        String goodsId  = request.getParameter("goodsId");
        String gongyiId  = request.getParameter("gongyiId");
        String saleId  = request.getParameter("saleId");
        iGoodsSaleService.updateByGongyiId(saleId, gongyiId);
        lnGongyigoodsService.deleteGongyiByGoods(goodsId);
        List<LnGongyigoods> lnGongyigongxuList=lnGongyigoodsService.selectGoodsGongyi(goodsId, "");
        if(lnGongyigongxuList.size()<1){
            LnGongyigoods lnGongyigoods=new LnGongyigoods();
            LnGongyi lnGongyi=lnGongyiService.selectById(gongyiId);
            lnGongyigoods.setGoodsId(goodsId);
            lnGongyigoods.setGongyiId(gongyiId);
            lnGongyigoods.setCode(lnGongyi.getCode());
            lnGongyigoods.setName(lnGongyi.getName());
            lnGongyigoods.setFile(lnGongyi.getFile());
            lnGongyigoods.setPhoto(lnGongyi.getPhoto());
            lnGongyigoods.setShuoming(lnGongyi.getShuoming());
            lnGongyigoodsService.insert(lnGongyigoods);
        }else if(lnGongyigongxuList.size()==1){
            LnGongyigoods lnGongyigoods=lnGongyigongxuList.get(0);
            LnGongyi lnGongyi=lnGongyiService.selectById(gongyiId);
            lnGongyigoods.setGoodsId(goodsId);
            lnGongyigoods.setGongyiId(gongyiId);
            lnGongyigoods.setCode(lnGongyi.getCode());
            lnGongyigoods.setName(lnGongyi.getName());
            lnGongyigoods.setFile(lnGongyi.getFile());
            lnGongyigoods.setPhoto(lnGongyi.getPhoto());
            lnGongyigoods.setShuoming(lnGongyi.getShuoming());
            lnGongyigoodsService.update(lnGongyigoods);
        }

        return  AjaxResult.success();
    }

}
